package com.aracky.system.controller;

import com.aracky.common.annotation.DataDeal;
import com.aracky.common.util.ConvertUtil;
import com.aracky.common.vo.CommonResult;
import com.aracky.common.vo.PageVO;
import com.aracky.system.constants.ComConstants;
import com.aracky.system.model.entity.SysBtnConfig;
import com.aracky.system.model.entity.SysMenu;
import com.aracky.system.model.form.SysMenuForm;
import com.aracky.system.model.request.SysMenuRequest;
import com.aracky.system.model.vo.SysMenuVO;
import com.aracky.system.service.SysBtnConfigService;
import com.aracky.system.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author zzp
 * @since 2022-07-14 11:22:57
 */
@RestController
@RequestMapping("/sysMenu")
@Api(tags = "菜单相关接口")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysBtnConfigService sysBtnConfigService;

    @PostMapping("/save")
    @ApiOperation(value = "新增菜单")
    public CommonResult saveMenu(@RequestBody SysMenuForm sysMenuForm) {
        sysMenuService.saveMenu(sysMenuForm);
        return CommonResult.success("菜单新增成功");
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改菜单")
    public CommonResult updateMenu(@RequestBody SysMenuForm sysMenuForm) {
        sysMenuService.updateMenu(sysMenuForm);
        return CommonResult.success("菜单修改成功");
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除菜单信息")
    @ApiImplicitParam(name = "menuIds", value = "菜单ids")
    public CommonResult deleteMenu(@RequestParam List<Long> menuIds) {
        sysMenuService.deleteMenu(menuIds);
        return CommonResult.success("菜单删除成功");
    }

    @DataDeal(user = true, dict = true)
    @GetMapping("/getMenu")
    @ApiOperation(value = "查询菜单详细")
    @ApiImplicitParam(name = "menuId", value = "菜单id")
    public CommonResult<SysMenuVO> getMenu(Long menuId) {
        SysMenu sysMenu = sysMenuService.getById(menuId);
        SysMenuVO sysMenuVO = ConvertUtil.sourceToTarget(sysMenu, SysMenuVO.class);

        //如果是按钮，查询配置信息
        if (ComConstants.MENU.TYPES[2].equals(sysMenu.getMenuType())) {
            SysBtnConfig btnConfig = sysBtnConfigService.getById(sysMenu.getMenuId());
            sysMenuVO.setBtnType(btnConfig.getBtnType());
            sysMenuVO.setOpenConditions(btnConfig.getOpenConditions());
            sysMenuVO.setApi(btnConfig.getApi());
            sysMenuVO.setIcon(btnConfig.getIcon());
            sysMenuVO.setPath(btnConfig.getPath());
            sysMenuVO.setTitle(btnConfig.getTitle());
        }

        return CommonResult.success(sysMenuVO);
    }

    @PostMapping("/pageList")
    @ApiOperation(value = "查询菜单分页信息(树形结构)")
    public CommonResult<PageVO<SysMenuVO>> getMenuPage(@RequestBody SysMenuRequest sysMenuRequest) {
        PageVO<SysMenuVO> pageVO = sysMenuService.getMenuPage(sysMenuRequest);
        return CommonResult.success(pageVO);
    }

    @GetMapping("/allList")
    @ApiOperation(value = "查询所有菜单(树形结构)")
    public CommonResult<List<SysMenuVO>> getMenuList() {
        List<SysMenuVO> menuList = sysMenuService.getMenuList();
        return CommonResult.success(menuList);
    }


}
